#include "widget.h"
#include "ui_serial_main.h"
#include "dev/inc/dev_serial.h"
#include "dev/inc/dev_debug.h"
#include <QMessageBox>
#include "dev/inc/dev_data_process.h"
DEV_SERIAL_STR my_serial;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    /*扫描串口并显示*/
    DEV_Scan_Serial_Port(&my_serial);
    ui->comboBox_serial_name->addItems(my_serial.serial_name_buf);
    /*初始化串口默认参数*/
    DEV_Set_Serial_Name(&my_serial, ui->comboBox_serial_name->currentText());
    DEV_Set_Serial_Baudrate(&my_serial, ui->comboBox_baudrate->currentText().toInt());
    DEV_Set_Serial_Databits(&my_serial, DEV_SERIAL_DATABITS_8);
    DEV_Set_Serial_Stopbits(&my_serial, DEV_SERIAL_STOPBITS_1);
    DEV_Set_Serial_Parity(&my_serial, DEV_SERIAL_PARITY_N);
    DEV_Set_Serial_Flowcontrol(&my_serial, 0);
    /*定义槽函数接收串口数据*/
    connect(&my_serial.serial_port, SIGNAL(readyRead()), this, SLOT(on_Serial_Ready_Read()));

}

Widget::~Widget()
{
    delete ui;
}

/*设置串口号*/
void Widget::on_comboBox_serial_name_currentIndexChanged(const QString &arg1)
{
    DEV_DEBUG_INFO(arg1);
    DEV_Set_Serial_Name(&my_serial, arg1);
}
/*设置波特率*/
void Widget::on_comboBox_baudrate_currentIndexChanged(const QString &arg1)
{
    int baudrate = arg1.toInt();
    DEV_DEBUG_INFO(baudrate);
    DEV_Set_Serial_Baudrate(&my_serial, baudrate);
}
/*设置数据位*/
void Widget::on_comboBox_databits_currentIndexChanged(const QString &arg1)
{
    DEV_DEBUG_INFO(arg1);
    if(arg1 == '8')
    {
        DEV_Set_Serial_Databits(&my_serial, DEV_SERIAL_DATABITS_8);
    }
    else if(arg1 == '7')
    {
        DEV_Set_Serial_Databits(&my_serial, DEV_SERIAL_DATABITS_7);
    }
    else if(arg1 == '6')
    {
        DEV_Set_Serial_Databits(&my_serial, DEV_SERIAL_DATABITS_6);
    }
    else if(arg1 == '5')
    {
        DEV_Set_Serial_Databits(&my_serial, DEV_SERIAL_DATABITS_5);
    }
    else
    {
        DEV_Set_Serial_Databits(&my_serial, DEV_SERIAL_DATABITS_8);
    }
}
/*设置停止位*/
void Widget::on_comboBox_stopbits_currentIndexChanged(int index)
{
    DEV_DEBUG_INFO(index);
    if(index == 0)
    {
        DEV_Set_Serial_Stopbits(&my_serial, DEV_SERIAL_STOPBITS_1);
    }
    else if(index == 1)
    {
        DEV_Set_Serial_Stopbits(&my_serial, DEV_SERIAL_STOPBITS_1_5);
    }
    else if(index == 2)
    {
        DEV_Set_Serial_Stopbits(&my_serial, DEV_SERIAL_STOPBITS_2);
    }
    else
    {
        DEV_Set_Serial_Stopbits(&my_serial, DEV_SERIAL_STOPBITS_1);
    }
}
/*设置校验位*/
void Widget::on_comboBox_parity_currentIndexChanged(int index)
{
    DEV_DEBUG_INFO(index);
    if(index == 0)
    {
        DEV_Set_Serial_Parity(&my_serial, DEV_SERIAL_PARITY_N);
    }
    else if(index == 1)
    {
        DEV_Set_Serial_Parity(&my_serial, DEV_SERIAL_PARITY_O);
    }
    else if(index == 2)
    {
        DEV_Set_Serial_Parity(&my_serial, DEV_SERIAL_PARITY_E);
    }
    else
    {
        DEV_Set_Serial_Parity(&my_serial, DEV_SERIAL_PARITY_N);
    }
}
/*设置流控*/
void Widget::on_comboBox_flow_currentIndexChanged(int index)
{
    DEV_DEBUG_INFO(index);
    if(index == 0)
    {
        DEV_Set_Serial_Flowcontrol(&my_serial, 0);
    }
    else if(index == 1)
    {
        DEV_Set_Serial_Flowcontrol(&my_serial, 1);
    }
    else
    {
        DEV_Set_Serial_Flowcontrol(&my_serial, 0);
    }
}
/*打开和关闭串口*/
void Widget::on_radioButton_open_serial_clicked(bool checked)
{
    DEV_DEBUG_INFO(checked);
    if(checked == true)
    {
        /*设置串口参数*/
        DEV_Serial_Config(&my_serial);
        if(DEV_Serial_Open(&my_serial) == DEV_SERIAL_ERROR_NONE)
        {
            /*改变显示*/
            ui->radioButton_open_serial->setText("关闭串口");
            /*禁止改变参数*/
            ui->comboBox_serial_name->setEnabled(false);
            ui->comboBox_baudrate->setEnabled(false);
            ui->comboBox_databits->setEnabled(false);
            ui->comboBox_stopbits->setEnabled(false);
            ui->comboBox_parity->setEnabled(false);
            ui->comboBox_flow->setEnabled(false);
            DEV_DEBUG_INFO("serial open ok!");
        }
        else
        {
            ui->radioButton_open_serial->setChecked(false);
            QMessageBox::information(NULL, "错误", "打开串口失败");
            DEV_DEBUG_INFO("serial open failed!");
        }
    }
    else
    {
        /*改变显示*/
        ui->radioButton_open_serial->setText("打开串口");
        /*允许改变参数*/
        ui->comboBox_serial_name->setEnabled(true);
        ui->comboBox_baudrate->setEnabled(true);
        ui->comboBox_databits->setEnabled(true);
        ui->comboBox_stopbits->setEnabled(true);
        ui->comboBox_parity->setEnabled(true);
        ui->comboBox_flow->setEnabled(true);
        if(DEV_Serial_Close(&my_serial) == DEV_SERIAL_ERROR_NONE)
        {
            DEV_DEBUG_INFO("serial close ok!");
        }
        else
        {
            DEV_DEBUG_INFO("serial close failed!");
        }
    }
}
/*发送数据*/
void Widget::on_pushButton_send_data_clicked()
{
    if(DEV_Get_Serial_Send_Data_Type(&my_serial) == DEV_SERIAL_DISPLAY_CHAR)
    {
        DEV_Serial_Send_Data(&my_serial, ui->lineEdit_send_data->text().toLatin1());
    }
    else
    {
        DEV_Serial_Send_Data(&my_serial, DEV_Qstring_To_Hex(ui->lineEdit_send_data->text().toLocal8Bit()));
    }
}
/*十六进制发送*/
void Widget::on_checkBox_hex_send_clicked(bool checked)
{
    if(checked == true)
    {
        DEV_Set_Serial_Send_Data_Type(&my_serial, DEV_SERIAL_DISPLAY_HEX);
    }
    else
    {
        DEV_Set_Serial_Send_Data_Type(&my_serial, DEV_SERIAL_DISPLAY_CHAR);
    }
}
/*十六进制接收*/
void Widget::on_checkBox_hex_recv_clicked(bool checked)
{
    if(checked == true)
    {
        DEV_Set_Serial_Display_Type(&my_serial, DEV_SERIAL_DISPLAY_HEX);
    }
    else
    {
        DEV_Set_Serial_Display_Type(&my_serial, DEV_SERIAL_DISPLAY_CHAR);
    }
}
/*接收数据*/
void Widget::on_Serial_Ready_Read(void)
{
    QByteArray recv_data;
    recv_data = DEV_Serial_Receive_Data(&my_serial);
    DEV_DEBUG_INFO(recv_data);
    if(DEV_Get_Serial_Display_Type(&my_serial) == DEV_SERIAL_DISPLAY_HEX)
    {
        ui->plainTextEdit_recv_data->appendPlainText(QString(recv_data.toHex()));
    }
    else
    {
        ui->plainTextEdit_recv_data->appendPlainText(QString(recv_data));
    }
}
/*清空接收*/
void Widget::on_pushButton_clear_recv_clicked()
{
    ui->plainTextEdit_recv_data->clear();
}
/*清空发送*/
void Widget::on_pushButton_clear_send_clicked()
{
    ui->lineEdit_send_data->clear();
}
